package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpResponse;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        // 取出当前登录的用户id
        Long empId = (Long) session.getAttribute("employee");
        // 补全创建人与修改人的id
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        // 调用service保存类型
        categoryService.save(category);
        return R.success("添加成功！");
    }

    /*
        菜品分配
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        // 把当前页和页大小传给
        Page<Category> pageResult = categoryService.findByPage(page,pageSize);
        return R.success(pageResult);
    }
    /*
        删除类别
     */
    @DeleteMapping
    public R delete(Long id){
        categoryService.deleteById(id);
        return R.success("删除类别成功");
    }

    @PutMapping
    public R update(@RequestBody Category category,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        // 修改人id
        category.setUpdateUser(empId);
        // 把category的值交给service
        categoryService.update(category);
        return R.success("修改成功");
    }

    /*
        导入Excel
        注意：如果需要做文件的下载，有两点要求，
            1. 返回值类型必须是void
            2. 方法上必须使用了@ResPonseBody注解，或者已经使用了@RestController
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 1.读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/cateory.xlsx");

        // 2.使用模板输入流创建工作簿
        Workbook workbook =new XSSFWorkbook(inputStream);

        // 3.得到工作单，得到第一行，并且提取样式存储到集合中
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        List<CellStyle> cellStyleList = new ArrayList<>();
        // 定义集合存储单元格样式
        for (int i = 1; i <= 5; i++) {
            // 得到每一个单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            // 存储到集合中
            cellStyleList.add(cellStyle);
        }
        // 4.得到所有的类别集合
        List<Category> categoryList = categoryService.findAll();
        // 5.遍历类别的集合，设置样式与数据
        for (int i = 0;i< categoryList.size();i++){
            // 得到一个类别，每一个类别就是一行数据
            Category category = categoryList.get(i);
            row = sheet.createRow(i + 1);
            // 创建单元格，并且设置单元格
            Cell cell = row.createCell(1);
            cell.setCellStyle(cellStyleList.get(0));
            cell.setCellValue(category.getType());

            cell = row.createCell(2);
            cell.setCellStyle(cellStyleList.get(1));
            cell.setCellValue(category.getName());

            cell = row.createCell(3);
            cell.setCellStyle(cellStyleList.get(2));
            cell.setCellValue(category.getSort());

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            cell = row.createCell(4);
            cell.setCellStyle(cellStyleList.get(3));
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));

            cell = row.createCell(5);
            cell.setCellStyle(cellStyleList.get(4));
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime() ));
        }
        // 6.输出工作簿即可，通知浏览器，当前是以文件下载的形式去处理内容
        response.setHeader("content-disposition","attachment;filename=category.xlsx");
        workbook.write(response.getOutputStream());
    }
    /*
        作用：根据type查询列表

     */
    @GetMapping("/list")
    public R list(Integer type){
        List<Category> categoryList = categoryService.list(type);
        return R.success(categoryList);
    }


}
